package timing.ukulele.auth.service;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.portal.view.UserVO;
import timing.ukulele.facade.portal.feign.UserFeignFacade;

import java.time.LocalDateTime;

@Service
public class OptService {

    private final PasswordEncoder passwordEncoder;
    @Resource
    private UserFeignFacade userService;

    public OptService(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }


    public ResponseData<String> bindUserOtp(String username, String password, String secret) {
        ResponseData<UserVO> res = userService.getUserByUserName(username);
        if (res == null || res.getData() == null)
            return ResponseData.error("绑定失败,用户名或密码错误");
        UserVO user = res.getData();

        // 已绑定的不能再绑定，需要先清除
        if (user.getIsOtp() != null && user.getIsOtp()) {
            return ResponseData.error("绑定失败，该用户已绑定");
        }

        // 校验用户名密码是否正确
        if (!passwordEncoder.matches(password, user.getPassword())) {
            return ResponseData.error("绑定失败,用户名或密码错误");
        }

        // 设置用户的多因素认证状态，并保存密钥
        ResponseData<String> bindRes = userService.bindOtp(user.getId(), secret);
        return bindRes;
    }


//    public static void main(String[] args) throws InterruptedException {
//        GoogleAuthenticator gAuth = new GoogleAuthenticator();
//        gAuth.createCredentials();
//        final GoogleAuthenticatorKey key = gAuth.createCredentials();
//        // 密钥
//        String secretKey = key.getKey();
//        System.out.println("密钥：" + secretKey);
//        String info = URLEncoder.encode("http://www.timingtech.top:admin", StandardCharsets.UTF_8);
//        String codeInfo = String.format("otpauth://totp/%s?secret=%s", info, secretKey);
//        System.out.println("二维码信息：" + codeInfo);
//
//
//        // 模拟客户端生成验证码
//        int code = gAuth.getTotpPassword(secretKey);
//        System.out.println("验证码：" + code);
//
//        Thread.sleep(20 * 1000);
//        // 服务器端验证
//        boolean isCodeValid = gAuth.authorize(secretKey, code);
//        System.out.println("20s验证：" + isCodeValid);
//
//        // 延迟时间,不一定
//        Thread.sleep(21 * 1000);
//        isCodeValid = gAuth.authorize(secretKey, code);
//        System.out.println("41s验证：" + isCodeValid);
//    }

    public static void main(String[] args) throws InterruptedException {
        GoogleAuthenticator gAuth = new GoogleAuthenticator();
        gAuth.createCredentials();
        final GoogleAuthenticatorKey key = gAuth.createCredentials();
        // 密钥
        String secretKey = key.getKey();
        gAuth.createCredentials();
        // 模拟客户端生成验证码
        for (int i = 0; i < 120; i++) {
            int code = gAuth.getTotpPassword(secretKey);
            System.out.println("验证码：" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss") + ":" + code);
            Thread.sleep(1 * 1000);
        }
    }
}
